#include <algorithm>
#include <cstdio>
using ll = long long;

const int N = 2e5 + 5, mod = 998244353;
int n, q, top;
int f[N], fa[N], stk[N], a[N], d[N];
int e_cnt, heads[N];
ll E[N], sqrE[N];

struct Edge {
  int v, nxt, w;
} e[N << 1];

inline void add(int u, int v, int w) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].w = w;
}

void dfs(int x) {
  static int ffa;
  stk[++top] = x;
  ffa = stk[std::max(1, top - d[x])];
  f[x] += a[x];
  if (f[x] >= mod) f[x] -= mod;
  f[fa[ffa]] -= a[x];
  if (f[fa[ffa]] < 0) f[fa[ffa]] += mod;
  for (int i = heads[x], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa[x]) {
      fa[v] = x;
      dfs(v);
      f[x] += f[v];
      if (f[x] >= mod) f[x] -= mod;
    }
  --top;
}

void pre(int x) {
  E[x] = f[x];
  sqrE[x] = 1ll * f[x] * f[x] % mod;
  for (int i = heads[x], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa[x]) {
      pre(v);
      sqrE[x] += (sqrE[v] * e[i].w % mod +
                  (E[x] * E[v] % mod * e[i].w % mod << 1) % mod) %
                 mod;
      if (sqrE[x] >= mod) sqrE[x] -= mod;
      E[x] += E[v] * e[i].w % mod;
      if (E[x] >= mod) E[x] -= mod;
    }
}

void solve(int x) {
  static ll faE, fasqrE;
  for (int i = heads[x], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa[x]) {
      faE = E[x] - E[v] * e[i].w % mod;
      if (faE < 0) faE += mod;
      fasqrE = sqrE[x] - sqrE[v] * e[i].w % mod;
      if (fasqrE < 0) fasqrE += mod;
      fasqrE -= (faE * E[v] % mod * e[i].w % mod * 2) % mod;
      if (fasqrE < 0) fasqrE += mod;
      sqrE[v] +=
          (fasqrE * e[i].w % mod + (E[v] * faE % mod * e[i].w % mod << 1)) %
          mod;
      if (sqrE[v] >= mod) sqrE[v] -= mod;
      E[v] += faE * e[i].w % mod;
      if (E[v] >= mod) E[v] -= mod;
      solve(v);
    }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("travel.in", "r", stdin);
  freopen("travel.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) { scanf("%d%d", &a[i], &d[i]); }
  for (int i = 1, u, v, w; i < n; ++i) {
    scanf("%d%d%d", &u, &v, &w);
    add(u, v, w);
  }
  dfs(1);
  pre(1);
  solve(1);
  scanf("%d", &q);
  for (int x; q--;) {
    scanf("%d", &x);
    printf("%lld\n", sqrE[x]);
  }
  return 0;
}